@echo off
REM Simple batch script to rebuild the application and package it into an installer

:: 初始化暂停模式参数
set "pause_mode=true"

:: 解析命令行参数
for %%a in (%*) do (
    if /i "%%a"=="/nopause" set "pause_mode=false"
)

REM Build the application using build.bat with nopause flag
cmd.exe /c build.bat /norun /nopause 

if %errorlevel% neq 0 (
    echo Error: Build failed!
    if "%pause_mode%" == "true" pause
    exit /b %errorlevel%
)

REM Compile installer with Inno Setup
echo Compiling installer with Inno Setup...
echo Current directory: %cd%

echo Trying to find Inno Setup...
set "iscc_path="

echo Checking: "%ProgramFiles(x86)%\Inno Setup 6\iscc.exe"
if exist "%ProgramFiles(x86)%\Inno Setup 6\iscc.exe" (
    set "iscc_path=%ProgramFiles(x86)%\Inno Setup 6\iscc.exe"
    echo Found Inno Setup at: %iscc_path%
) else (
    echo Checking: "%ProgramFiles%\Inno Setup 6\iscc.exe"
    if exist "%ProgramFiles%\Inno Setup 6\iscc.exe" (
        set "iscc_path=%ProgramFiles%\Inno Setup 6\iscc.exe"
        echo Found Inno Setup at: %iscc_path%
    )
)

if "%iscc_path%"=="" (
    echo Error: Inno Setup not found!
    echo Please make sure Inno Setup 6 is installed.
    if "%pause_mode%" == "true" pause
    exit /b 1
)

echo Running command: "%iscc_path%" "simple_installer.iss"
"%iscc_path%" "simple_installer.iss"

if %errorlevel% neq 0 (
    echo Error: Installer compilation failed!
    if "%pause_mode%" == "true" pause
    exit /b %errorlevel%
)

REM Check if installer was created
echo Checking installer...
if exist .\bin\Release\net8.0-windows\Toolbox-Setup-win-x64-1.0.0.exe (
    echo.
    echo Success! Installer created at .\bin\Release\net8.0-windows\Toolbox-Setup-win-x64-1.0.0.exe
) else (
    echo.
    echo Error: Installer not found!
    if "%pause_mode%" == "true" pause
    exit /b 1
)

if "%pause_mode%" == "true" pause